<?php
  /**
   * @author EVANDRO DE OLIVEIRA.
   * @category LIBRARY
   * @package CORE.
   * @since 09/07/2012.
   * @last_update 09/07/2012.
   * @copyright 2012.
   * @version 1.0.
   */
  class Core_ZendLog{
  	/**
  	 * Método Construct
  	 * @param string Mensagem a ser gravada no log.
  	 * @param string Nome do arquivo de log
  	 * @param string Tipo da mensagem do log. Os tipos possíveis são
  	 *               EMERG   = 0;  // Emergency: system is unusable
  	 *               ALERT   = 1;  // Alert: action must be taken immediately
  	 *               CRIT    = 2;  // Critical: critical conditions
  	 *               ERR     = 3;  // Error: error conditions
  	 *               WARN    = 4;  // Warning: warning conditions
  	 *               NOTICE  = 5;  // Notice: normal but significant condition
  	 *               INFO    = 6;  // Informational: informational messages
  	 *               DEBUG   = 7;  // Debug: debug messages
  	 */
  	public function __construct($strMensagem, $strLogName = NULL, $strType = 'INFO'){  		
      switch($strType){
        case 'EMERG':
        case '0':
          $intType = '0';
        break;
        
        case 'ALERT':
        case '1':
          $intType = '1';
        break;
        
        case 'CRIT':
        case '2':
          $intType = '2';
        break;
        
        case 'ERR':
        case '3':
          $intType = '3';
        break;
        
        case 'WARN':
        case '4':
          $intType = '4';
        break;
        
        case 'NOTICE':
        case '5':
          $intType = '5';
        break;
        
        case 'INFO':
        case '6':
          $intType = '6';
        break;
        
        case 'DEBUG':
        case '7':
          $intType = '7';
        break;
        
        default:
          $intType = '6';
      }
      
      if(APPLICATION_ENV == 'development'){
      	$objWriterFirebug = new Zend_Log_Writer_Firebug();
        $objLogger = new Zend_Log($objWriterFirebug);
        
        $objRequest = new Zend_Controller_Request_Http();
        $objResponse = new Zend_Controller_Response_Http();
        
        $objChannel = Zend_Wildfire_Channel_HttpHeaders::getInstance();
        $objChannel->setRequest($objRequest);
        $objChannel->setResponse($objResponse);
        
        ob_start();
        
        $objLogger->log($strMensagem, $intType);
        $objChannel->flush();
        $objResponse->sendHeaders();
      }
      
      if($strLogName == null){
      	$strLogName = Zend_Registry::getInstance()->config->log->name;
      }
      
      $strFileName = $strLogName . "_" . date("Ymd") . ".log";            
      $objWriter = new Zend_Log_Writer_Stream(Zend_Registry::getInstance()->config->log->path . $strFileName);
      $objLogger = new Zend_Log($objWriter);
      $objLogger->log($strMensagem, $intType);
  	}
  }